<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
            <el-form-item label="商品标题:" prop="title" label-width="150">
                <el-input
                    v-model="queryParams.title"
                    placeholder="请输入商品标题"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="居民:" prop="peopleId">
                <el-input
                    v-model="queryParams.peopleName"
                    placeholder="请输入居民名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="手机号:" prop="peopleId" label-width="120">
                <el-input
                    v-model="queryParams.mobile"
                    placeholder="请输入居民手机号"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item label="发放人:" prop="verificationPeopleName">
                <el-input
                    v-model="queryParams.verificationPeopleName"
                    placeholder="请输入发放人"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="兑换状态:" prop="audit" label-width="100">
                <el-select v-model="queryParams.status" placeholder="兑换状态" clearable>
                    <el-option
                        v-for="dict in dict.type.exchange_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="预计发放时间:" prop="exchangeStartTime" label-width="120">
                <el-date-picker
                    v-model="exchangeDate.date"
                    type="datetimerange"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    range-separator="至"
                    start-placeholder="预计发放开始时间"
                    end-placeholder="预计发放结束时间"
                    :default-time="['00:00:00', '23:59:59']"
                    @change="exchangeDateFormat"
                ></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                    type="primary"-->
<!--                    plain-->
<!--                    icon="el-icon-plus"-->
<!--                    size="mini"-->
<!--                    @click="handleAdd"-->
<!--                    v-hasPermi="['business:goodsExchange:add']"-->
<!--                >新增-->
<!--                </el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                    type="success"-->
<!--                    plain-->
<!--                    icon="el-icon-edit"-->
<!--                    size="mini"-->
<!--                    :disabled="single"-->
<!--                    @click="handleUpdate"-->
<!--                    v-hasPermi="['business:goodsExchange:edit']"-->
<!--                >修改-->
<!--                </el-button>-->
<!--            </el-col>-->
            <!--<el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['business:goodsExchange:remove']"
                >删除
                </el-button>
            </el-col>-->
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['business:goodsExchange:export']"
                >导出兑换记录
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="goodsExchangeList" @selection-change="handleSelectionChange" border>
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="村社" align="center" prop="communityName"/>
            <el-table-column label="类目" align="center" prop="classifyName"/>
            <el-table-column label="商品标题" align="left" prop="title" min-width="300" :show-overflow-tooltip="true"/>
<!--            <el-table-column label="商品图" align="center" prop="imgUrl" width="100">-->
<!--                <template slot-scope="scope">-->
<!--                    <image-preview :src="scope.row.imgUrl" :width="50" :height="50"/>-->
<!--                </template>-->
<!--            </el-table-column>-->
            <el-table-column label="兑换时间" align="center" prop="receivedTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.receivedTime) }}</span>
                </template>
            </el-table-column>

            <el-table-column label="预期发放的时间" align="center" prop="exchangeStartTime" width="300">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.exchangeStartTime, '{y}-{m}-{d} {h}:{i}') }}
                        至
                        {{ parseTime(scope.row.exchangeEndTime,'{y}-{m}-{d} {h}:{i}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="消耗积分" align="center">
                <template slot-scope="scope">
                    <el-tag type="danger">{{ scope.row.exchangeNum }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="居民" align="center" prop="peopleName"/>
            <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status===0">待领取</el-tag>
                    <el-tag type="success" v-if="scope.row.status===1">已领取</el-tag>
                    <el-tag type="success" v-if="scope.row.status===2">已取消</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="发放地址" align="left" prop="exchangeAddress" :show-overflow-tooltip="true"/>
            <el-table-column label="核销时间" align="center" prop="verificationTime">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.verificationTime,'{y}-{m}-{d} {h}:{i}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="发放人" align="center" prop="verificationPeopleName"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-view"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['business:goodsExchange:edit']"
                    >查看
                    </el-button>
                    <el-button
                        v-if="scope.row.status===0"
                        size="mini"
                        type="text"
                        icon="el-icon-c-scale-to-original"
                        @click="handleCancel(scope.row)"
                        v-hasPermi="['business:goodsExchange:cancel']"
                    >取消兑换
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改商品记录对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
            <el-descriptions  title="兑换信息"  size="medium" border  direction="horizontal" :column="2">
                <el-descriptions-item label="村社">{{form.communityName}}</el-descriptions-item>
                <el-descriptions-item label="类目">{{form.classifyName}}</el-descriptions-item>
                <el-descriptions-item label="商品名称" :span="2">{{form.title}}</el-descriptions-item>
                <el-descriptions-item label="兑换人">{{form.peopleName}}</el-descriptions-item>
                <el-descriptions-item label="消耗积分"><el-tag type="danger">-{{form.exchangeNum}}</el-tag></el-descriptions-item>
                <el-descriptions-item label="兑换时间">{{parseTime(form.receivedTime)}}</el-descriptions-item>
                <el-descriptions-item label="状态">
                    <el-tag v-if="form.status==0">待发放</el-tag>
                    <el-tag type="success" v-if="form.status==1">已发放</el-tag>
                    <el-tag type="danger" v-if="form.status==2">已取消</el-tag>
                </el-descriptions-item>
                <el-descriptions-item label="预计发放时间" :span="2">{{ parseTime(form.exchangeStartTime) }} 至 {{ parseTime(form.exchangeEndTime) }}</el-descriptions-item>
                <el-descriptions-item label="发放地址" :span="2">{{ form.exchangeAddress }}</el-descriptions-item>
                <el-descriptions-item label="发放人">{{ form.verificationPeopleName }}</el-descriptions-item>
                <el-descriptions-item label="发放时间">{{ parseTime(form.verificationTime) }}</el-descriptions-item>
            </el-descriptions>
        </el-dialog>
    </div>
</template>

<script>
import {
    listGoodsExchange,
    getGoodsExchange,
    delGoodsExchange,
    addGoodsExchange,
    updateGoodsExchange, cancelUserExchange
} from "@/api/business/goodsExchange";

export default {
    name: "GoodsExchange",
    dicts:['exchange_status'],
    data() {
        return {
            // 日期范围
            exchangeDate:{
                date:[]
            },
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 商品记录表格数据
            goodsExchangeList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                sourceId: null,
                peopleId: null,
                title: null,
                classifyId: null,
                communityId: null,
                exchangeNum: null,
                receivedTime: null,
                verificationTime: null,
                exchangeStartTime: null,
                exchangeEndTime: null,
                status: null,
                exchangeAddress: null,
                verificationPeopleId: null,
                verificationPeopleName: null,
                imgUrl: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {}
        };
    },
    created() {
        this.getList();
    },
    methods: {
        exchangeDateFormat(exchange) {
            this.queryParams.exchangeStartTime = exchange[0].toString()
            this.queryParams.exchangeEndTime = exchange[1].toString()
        },
        /** 查询商品记录列表 */
        getList() {
            this.loading = true;
            listGoodsExchange(this.queryParams).then(response => {
                this.goodsExchangeList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                userGoodsId: null,
                sourceId: null,
                peopleId: null,
                title: null,
                classifyId: null,
                communityId: null,
                exchangeNum: null,
                receivedTime: null,
                verificationTime: null,
                exchangeStartTime: null,
                exchangeEndTime: null,
                status: null,
                exchangeAddress: null,
                verificationPeopleId: null,
                verificationPeopleName: null,
                imgUrl: null,
                createTime: null,
                updateTime: null,
                createBy: null,
                updateBy: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.userGoodsId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加商品记录";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const userGoodsId = row.userGoodsId || this.ids
            getGoodsExchange(userGoodsId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "查看商品兑换记录";
            });
        },
        /**
         * 取消兑换
         */
        handleCancel(row) {
            const userGoodsIds = row.userGoodsId || this.ids;
            this.$modal.confirm('确认取消"' + row.peopleName+ '"的'+row.title+'兑换记录?',"取消兑换提示").then(function () {
                return cancelUserExchange(userGoodsIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("取消兑换成功");
            }).catch(() => {
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.userGoodsId != null) {
                        updateGoodsExchange(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addGoodsExchange(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const userGoodsIds = row.userGoodsId || this.ids;
            this.$modal.confirm('是否确认删除商品记录编号为"' + userGoodsIds + '"的数据项？').then(function () {
                return delGoodsExchange(userGoodsIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('business/goodsExchange/export', {
                ...this.queryParams
            }, `商品兑换记录_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
